﻿module Sustainalytics.Governance {

    import Common = Sustainalytics.Common;

    export class GovernanceProfileService implements IGovernanceProfileService {

        private $q: ng.IQService;
        private $http: ng.IHttpService;
        private notificationFactory: Common.INotificationFactory;
        private staticObjects: Common.IStaticObjects;
        private $location: ng.ILocationService;

        //Ctor.
        constructor(
            $q: ng.IQService,
            $http: ng.IHttpService,
            notificationFactory: Common.INotificationFactory,
            staticObjects: Common.IStaticObjects,
            $location: ng.ILocationService) {

            this.$q = $q;
            this.$http = $http;
            this.notificationFactory = notificationFactory;
            this.$location = $location;
        }

        private governanceProfilesUrl(): string {
            var ctx = this;
            return ctx.$location.protocol() + "://" + ctx.$location.host() + ":" + ctx.$location.port() + "/governanceserv/api/v1/governance/";
        }

        private governanceFilesUrl(): string {
            var ctx = this;
            return ctx.$location.protocol() + "://" + ctx.$location.host() + ":" + ctx.$location.port() + "/governanceserv/api/v1/files/";
        }

        // show error message for a given error code
        private showError(err) {
            var ctx = this;
            if (err && err['code'])
                ctx.notificationFactory.error('', ctx.staticObjects.getErrorMesageByStatusCode(err, err['code']));
        }

        private showMessage(msg) {
            var ctx = this;
            ctx.notificationFactory.success(msg);
        }

        public getGovernanceProfile(id: string): ng.IPromise<GovernanceProfileDto> {

            var ctx = this;
            var def = this.$q.defer();
            var promise = this.$http.get(ctx.governanceProfilesUrl() + id, { } );
            promise
                .success((data) => { def.resolve(data); })
                .error((data, status, headers, config) => { def.reject({ data: data, status: status, headers: headers, config: config }); });

            return def.promise;
        }

    }

    angular.module(governanceModuleName).service('GovernanceProfileService',
        ['$q', '$http', 'notificationFactory', 'staticObjects','$location', GovernanceProfileService]);
}